<template>
  <div class="mapDiv">
    <div class="header">
      <div class="title2">
        <div class="title-left">
          <img src="@/assets/map.png" /><span>底图底数</span>
        </div>
        <div class="trangle" />
      </div>
      <el-select
        v-model="selectValue"
        placeholder="请选择"
        class="elselect"
        @change="selectchange"
      >
        <el-option
          v-for="item in citylist"
          :key="item.id"
          :label="item.label"
          :value="item.label"
        >
        </el-option>
      </el-select>
    </div>
    <div ref="chart" class="map"></div>
    <div class="backicon" v-show="backshow" @click="bachtohn">
      <i class="el-icon-back"></i>
    </div>
  </div>
</template>
<script>
import Bus from "@/bus.js";
import ResizeChart from "./mixin/ResizeChart";
import {
  mapGetters
} from 'vuex'
export default {
  computed: {
    ...mapGetters(["citylist"]),
  },
  data() {
    return {
      selectValue: "",
      myChart: null,
      jsonvalue: [],
      backshow: false,
      option: {
        tooltip: {
          trigger: "item",
          showDelay: 0,
          transitionDuration: 0.2,
          formatter: function (params) {
            return params.name;
          },
        },
        series: [
          {
            name: "",
            type: "map",
            roam: false,
            aspectScale: 0.9,
            mapType: "湖南省", // 自定义扩展图表类型
            emphasis: {
              itemStyle: {
                areaColor: "#4778c7",
              },
            },
            label: {
              show: true,
              formatter: function (params) {},
            },
            data: [],
            // 自定义名称映射
            nameMap: {},
          },
        ],
      },
      mapList: [
        "湖南省",
        "长沙市",
        "株洲市",
        "湘潭市",
        "常德市",
        "衡阳市",
        "怀化市",
        "娄底市",
        "邵阳市",
        "永州市",
        "岳阳市",
        "益阳市",
        "张家界市",
        "郴州市",
        "湘西土家族苗族自治区",
      ],
    };
  },
  mounted() {
    this.getEchartData();
  },
  mixins: [ResizeChart],

  methods: {
    selectchange() {
      this.option.series[0].mapType = this.selectValue;
      this.myChart.setOption(this.option);
      Bus.$emit("witchCity", city.name);
    },
    bachtohn() {
      this.option.series[0].mapType = "湖南省";
      this.myChart.setOption(this.option);
      this.selectValue = "";
      Bus.$emit("witchCity", "湖南省");
    },
    getEchartData() {
      this.myChart = this.$echarts.init(this.$refs.chart);
      this.myChart.showLoading();
      this.mapList.map((v) => {
        let mapJson = require(`@/assets/json/${v}.json`);
        this.$echarts.registerMap(v, mapJson);
      });
      this.myChart.setOption(this.option);
      this.myChart.on("click", (city) => {
        this.backshow = true;
        if (this.mapList.indexOf(city.name) == -1) {
          return;
        }
        this.selectValue = city.name;
        Bus.$emit("witchCity", city.name);
        this.myChart.showLoading();
        this.option.series[0].mapType = city.name;
        this.myChart.setOption(this.option);
        this.myChart.hideLoading();
      });
      this.myChart.hideLoading();
    },
  },
};
</script>
<style lang="scss" scoped>
.mapDiv {
  position: relative;
  .header {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .elselect {
      margin-right: 10px;
      border-radius: 3px;
      ::v-deep .el-input {
        border-radius: 3px;
        .el-input__inner {
          background-color: #f5f5f5 !important;
          color: #4778c7 !important;
        }
        .el-input__inner::-webkit-input-placeholder {
          color: #4778c7 !important;
        }
      }
    }

    .title2 {
      width: 35%;
      height: 100%;
      color: #fff;
      font-size: 18px;
      letter-spacing: 1px;
      display: flex;
      align-items: center;
      position: relative;
      .title-left {
        display: flex;
        background-color: #4778c7;
        height: 40px;
        width: calc(100% - 40px);
        padding: 10px;
        align-items: center;
      }
      img {
        width: 30px;
        height: 30px;
        margin-right: 10px;
      }
      .trangle {
        float: left;
        width: 0px;
        height: 0px;
        border-left: 40px solid #4778c7;
        border-top: 40px solid #4778c7;
        border-right: 40px solid transparent;
      }
    }
  }
  .map {
    margin: 10px;
    width: calc(100% - 20px);
    height: calc(100% - 60px);
    border: 1px dashed #4778c7;
    border-radius: 5px;
    position: relative;
  }
  .backicon {
    position: absolute;
    top: 60px;
    left: 20px;
    font-size: 20px;
    font-weight: 600;
    color: #fff;
    padding: 5px;
    z-index: 9999;
    border-radius: 50%;
    background-color: #4778c7;
  }
}
</style>
